package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.module.basic.service.BoardService;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.dao.HOrderDetailDao;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:明细表/子表
 * @author: th
 * @time: 2020/3/23 13:28
 */
@Service
public class HOrderDetailService {

    @Autowired
    private HOrderDetailDao hOrderDetailDao;
    @Autowired
    private HBasicService HBasicService;
    @Autowired
    private BoardService boardService;
    @Autowired
    private HInventoryDao hInventoryDao;

    public HOrderDetailModel saveHOrderDetail(HOrderDetailModel hOrderDetailModel) {

        //保存入库单明细
        HOrderDetailModel hOrderDetail = new HOrderDetailModel();

        HInventory hInventory = hInventoryDao.selectAllById(hOrderDetailModel.getItyId());
        hOrderDetail.setOdaRushId(hOrderDetailModel.getOdaRushId());//红冲用   生成单据的子单ID
        hOrderDetail.setOdaOdrId(hOrderDetailModel.getOdrId());//获取主单ID
        if(hOrderDetailModel.getOdaSapCode() != null){
            hOrderDetail.setOdaSapCode(hOrderDetailModel.getOdaSapCode().replace("\n", "").replace(" ", ""));//SAP物料编码
        }
        if(hOrderDetailModel.getOdaMatName() !=null){
            hOrderDetail.setOdaMatName(hOrderDetailModel.getOdaMatName().trim().replace("\n", "").replace(" ", ""));//名称、物料名称
        }
        if(hOrderDetailModel.getOdaMatCode() != null){
            hOrderDetail.setOdaMatCode(hOrderDetailModel.getOdaMatCode().replace("\n", "").replace(" ", ""));//物料编码
        }
        if(hOrderDetailModel.getOdaDrawingCode() != null){
            hOrderDetail.setOdaDrawingCode(hOrderDetailModel.getOdaDrawingCode().trim().replace("\n", "").replace(" ", ""));//图号
        }
        hOrderDetail.setOdaNum(hOrderDetailModel.getOdaAvaNum());//数量
        if(hOrderDetailModel.getOdaUnit() != null){
            hOrderDetail.setOdaUnit(hOrderDetailModel.getOdaUnit().replace("\n", "").replace(" ", ""));//单位
        }
        hOrderDetail.setOdaPrice(hOrderDetailModel.getOdaPrice());//单价
        hOrderDetail.setOdaAmount(hOrderDetailModel.getOdaAmount());
        if(hOrderDetailModel.getOdaTrackCode() != null){
            hOrderDetail.setOdaTrackCode(hOrderDetailModel.getOdaTrackCode().trim().replace("\n", "").replace(" ", ""));//跟踪号
        }
        if(hOrderDetailModel.getOdaProCode() != null) {
            hOrderDetail.setOdaProCode(boardService.projectCodeHandle(hOrderDetailModel.getOdaProCode()));//项目编号
        }
        if(hOrderDetailModel.getOdaProName() != null){
            hOrderDetail.setOdaProName(hOrderDetailModel.getOdaProName().trim().replace("\n", "").replace(" ", ""));//项目名称
        }
        hOrderDetail.setOdaPart(hOrderDetailModel.getOdaPart());//部位
        if(hOrderDetailModel.getOdaBrand() != null){
            hOrderDetail.setOdaBrand(hOrderDetailModel.getOdaBrand().trim().replace("\n", "").replace(" ", ""));//品牌
        }
        hOrderDetail.setOdaMakingPiecesStatus(hOrderDetailModel.getOdaMakingPiecesStatus());//存放状态
        hOrderDetail.setOdaPosition(hOrderDetailModel.getOdaPosition());//区位
        hOrderDetail.setOdaRedFlag(hOrderDetailModel.getOdaRedFlag());//红冲标识
        if(("外购件").equals(hOrderDetailModel.getOdaMatTypeName())){
            hOrderDetail.setOdaMatType(1);
        }else if(("外协件").equals(hOrderDetailModel.getOdaMatTypeName())){
            hOrderDetail.setOdaMatType(2);
        }else {
            hOrderDetail.setOdaMatType(hOrderDetailModel.getOdaMatType());
        }
        if(hOrderDetailModel.getOdaWhiId() == null) {
            hOrderDetail.setOdaWhiId(HBasicService.getWarIdByName(hOrderDetailModel.getOdaWhiIdName()));
        }else {
            hOrderDetail.setOdaWhiId(hOrderDetailModel.getOdaWhiId());
        }
        if(("库存在库").equals(hOrderDetailModel.getOdaStockTypeName())){
            hOrderDetail.setOdaStockType(1);//在库类型 1是库存在库，2是项目在库
        }else if(("项目在库").equals(hOrderDetailModel.getOdaStockTypeName())){
            hOrderDetail.setOdaStockType(2);
        }else {
            hOrderDetail.setOdaStockType(hOrderDetailModel.getOdaStockType());
        }
        if(("成品件").equals(hOrderDetailModel.getOdaOutsourcedTypeName())){
            hOrderDetail.setOdaOutsourcedType(1);
        }else if(("毛坯件").equals(hOrderDetailModel.getOdaOutsourcedTypeName())){
            hOrderDetail.setOdaOutsourcedType(2);
        }
        hOrderDetail.setOdaRemark(hOrderDetailModel.getOdaRemark());//备注
        hOrderDetail.setOdaStorageDate(hOrderDetailModel.getOdaStorageDate());//入库时间
        hOrderDetail.setOdaCaseNo(hOrderDetailModel.getOdaCaseNo());//箱号
        hOrderDetail.setOdaBoxingDate(hOrderDetailModel.getOdaBoxingDate());//装箱日期
        if (("是").equals(hOrderDetailModel.getOdaIfFinishedProductName())) {
            hOrderDetail.setOdaIfFinishedProduct(1);
        } else if (("否").equals(hOrderDetailModel.getOdaIfFinishedProductName())) {
            hOrderDetail.setOdaIfFinishedProduct(0);
        } else {
            hOrderDetail.setOdaIfFinishedProduct(hOrderDetailModel.getOdaIfFinishedProduct());
        }
        hOrderDetail.setOdaWeight(hOrderDetailModel.getOdaWeight());//重量
        hOrderDetail.setOdaOrderNum(hOrderDetailModel.getOdaOrderNum());//订单号
        hOrderDetail.setOdaTexture(hOrderDetailModel.getOdaTexture());//材质
        hOrderDetail.setOdaSpec(hOrderDetailModel.getOdaSpec());//规格
        hOrderDetail.setOdaOutItyId(hOrderDetailModel.getItyId());
        if(hInventory!=null && hInventory.getItyPrice()!=null){
            hOrderDetail.setOdaPrice(hInventory.getItyPrice());//单价
            if(hOrderDetailModel.getOdaMatType() == 4){
                hOrderDetail.setOdaAmount(hOrderDetail.getOdaWeight()*hOrderDetail.getOdaPrice());
            }else if(hOrderDetailModel.getOdaMatType() == 2){
                hOrderDetail.setOdaAmount(hOrderDetail.getOdaNum()*hOrderDetail.getOdaPrice());
            }
        }
        hOrderDetail.setOdaSupplier(hOrderDetailModel.getOdaSupplier());//供应商
        hOrderDetail.setContractNo(hOrderDetailModel.getContractNo());//合同编号
        //保存入库单明细
        hOrderDetailDao.insert(hOrderDetail);
        hOrderDetailModel.setOdaId(hOrderDetail.getOdaId());
        return hOrderDetailModel;
    }
}
